package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.common.exception.NoSuchIdException;
import com.heima.mapper.RegionMapper;
import com.heima.mapper.TaskMapper;
import com.heima.mapper.UserMapper;
import com.heima.mapper.VmMapper;
import com.heima.pojo.*;
import com.heima.pojo.dto.CreTaskDto;
import com.heima.pojo.dto.TaskPageDto;
import com.heima.pojo.vo.*;
import com.heima.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private VmMapper vmMapper;
    @Autowired
    private RegionMapper regionMapper;
    /**
     * 工单统计
     * @param first
     * @param last
     * @return
     */
    @Override
    public List<TaskReportInfoVo> taskReportInfo(Date first, Date last) {
        List<TaskReportInfoVo> vos = taskMapper.taskReportInfo(first, last);

        return vos;
    }

    /**
     * 新建工单
     * @param dto
     */
    @Override
    public void createTask(CreTaskDto dto) {
        Task task = new Task();
        BeanUtils.copyProperties(dto,task);
        //根据userId拿到username
        User user = userMapper.queryById(dto.getUserId());
        if (user == null){
            throw new NoSuchIdException("用户不存在");
        }
        //根据设备编号得到所在地区
        VendingMachine vendingMachine = vmMapper.queryByInnerCode(dto.getInnerCode());
        if (vendingMachine == null){
            log.info("设备编号不存在");
        }
        task.setUsername(user.getUserName());
        task.setCreateTime(LocalDateTime.now());
        task.setUpdateTime(LocalDateTime.now());
        task.setCreateType(1);
        task.setRegionId(vendingMachine.getRegionId());
        taskMapper.insert(task);
    }

    /**
     * 取消工单
     * @param taskId
     * @param dto
     */
    @Override
    public void cancelTask(Integer taskId,CreTaskDto dto) {
        //根据工单id查询工单
        Task task = taskMapper.queryById(taskId);
        if (task == null){
            throw new NoSuchIdException("工单不存在");
        }
        task.setDesc(dto.getDesc());
        task.setUpdateTime(LocalDateTime.now());
        taskMapper.update(task);
    }

    /**
     * 工单统计
     * @param start
     * @param end
     * @return
     */
    @Override
    public List<TaskCountVo> collectReport(Date start, Date end) {
        List<TaskCountVo> vos = taskMapper.collectReport(start, end);
        return vos;
    }

    /**
     * 工作排名
     * @param start
     * @param end
     * @param isRepair
     * @param regionId
     * @return
     */
    @Override
    public List<UserWorkCountVo> userWorkTop10(Date start, Date end, Boolean isRepair, String regionId) {
        Integer flag;
        if (isRepair){
            flag = 1;
        }else {
            flag = 2;
        }
        if (regionId == "0"){
            regionId = null;
        }
        return taskMapper.userWorkTop10(start,end,flag,regionId);
    }


    /**
     * 工单分页
     * @param dto
     * @return
     */
    @Override
    public TaskPageVo pageQueryRepair(TaskPageDto dto) {
        dto.check();
        if (dto.getIsRepair()){
            dto.setFlag(1);
        }else {
            dto.setFlag(0);
        }
        PageHelper.startPage(dto.getPageIndex(),dto.getPageSize());
        Page<TaskVo> taskVos = taskMapper.pageQueryRepair(dto);
        taskVos.forEach(taskVo -> {
            TaskType taskType = new TaskType();
            taskType.setTypeName(taskVo.getTypeName());
            taskType.setTypeId(taskVo.getTypeId());
            taskType.setType(taskVo.getType());
            taskVo.setTaskType(taskType);
        });
        TaskPageVo taskPageVo = new TaskPageVo();
        taskPageVo.setCurrentPageRecords(taskVos);
        taskPageVo.setPageIndex(dto.getPageIndex());
        taskPageVo.setPageSize(dto.getPageSize());
        taskPageVo.setTotalPage(taskVos.getPages());
        taskPageVo.setTotalCount(taskVos.getTotal());
        return taskPageVo;
    }

    /**
     * 获取所有工单类型
     * @return
     */
    @Override
    public List<TaskType> queryAllType() {
        return taskMapper.queryAllType();
    }

    /**
     * 获取所有工单状态
     * @return
     */
    @Override
    public List<TaskStatusVo> queryAllStatus() {
        return taskMapper.queryAllStatus();
    }

    /**
     * 获取工单详情
     * @param taskId
     * @return
     */
    @Override
    public Task queryTaskInfo(Integer taskId) {
        return taskMapper.queryTaskInfo(taskId);
    }

    /**
     * 获取补货预警值
     * @param alertValue
     * @return
     */
    @Override
    public Integer getSupplyAlertValue(Integer alertValue) {
        if (alertValue> 100){
            throw new RuntimeException("预警值超过100%");
        }
        return alertValue;
    }

    /**
     * 补货详情
     */
    @Override
    public Task getSupplyInfo(Integer taskId) {
        Task task = taskMapper.getSupplyInfo(taskId);
        List<TaskDetail> details = taskMapper.getTaskDetails(taskId);
        if (CollectionUtils.isEmpty(details)){
            log.info("该工单没有明细内容");
        }else {
            task.setDetails(details);
        }
        return task;
    }

    /**
     * 获取维修次数
     */
    @Override
    public Integer getRepairTimes(Date start, Date end, String innerCode) {
        return taskMapper.getRepairTimes(start,end,innerCode);
    }
}
